package com.cloudcc.mobile.bull;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.basebean.BaseListEntity;
import com.cloudcc.mobile.bull.adapter.AdapterDate;
import com.cloudcc.mobile.bull.adapter.AdapterWeek;
import com.cloudcc.mobile.bull.bean.CheckAdapterkBean;
import com.cloudcc.mobile.bull.bean.CheckinHistoryBean;
import com.cloudcc.mobile.bull.listener.OnSignedSuccess;
import com.cloudcc.mobile.bull.model.impl.GongniuCheckHistoryNewModel;
import com.cloudcc.mobile.bull.presenter.impl.GongniuCheckHistoryNewPresenter;
import com.cloudcc.mobile.bull.view.IGongniuCheckHistoryNewView;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.util.DateUtil;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.weight.InnerGridView;
import com.mypage.utils.UtilsShowDialog;
import com.mypage.view.datedialog.DateTimeUtil;

import org.kymjs.kjframe.ui.ViewInject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 92152
 *     e-mail : xxx@xx
 *     time   : 2019/09/12
 *     desc   : 新版考勤签到
 *     version: 1.0
 * </pre>
 */
public class GongniuCheckinHistoryNewActivity extends BaseActivity implements IGongniuCheckHistoryNewView {
    @Bind(R.id.textUser)
    TextView textUser;
    @Bind(R.id.tvYear)
    TextView tvYear;
    @Bind(R.id.gvWeek)
    InnerGridView gvWeek;
    @Bind(R.id.gvDate)
    InnerGridView gvDate;
    @Bind(R.id.sbworkName)
    TextView sbworkName;
    @Bind(R.id.sbtime)
    TextView sbtime;
    @Bind(R.id.sbcontext)
    TextView sbcontext;
    @Bind(R.id.am_ll)
    RelativeLayout amLl;
    @Bind(R.id.zwworkName)
    TextView zwworkName;
    @Bind(R.id.zwtime)
    TextView zwtime;
    @Bind(R.id.zwcontext)
    TextView zwcontext;
    @Bind(R.id.noon_ll)
    RelativeLayout noonLl;
    @Bind(R.id.xbworkName)
    TextView xbworkName;
    @Bind(R.id.xbtime)
    TextView xbtime;
    @Bind(R.id.xbcontext)
    TextView xbcontext;
    @Bind(R.id.pm_ll)
    RelativeLayout pmLl;

    int yearSelect,monthSelect;
    private GongniuCheckHistoryNewPresenter presenter;
    private UtilsShowDialog showDialog;
    private ArrayList<CheckAdapterkBean> checkAdapterkBeans = new ArrayList<>();
    private AdapterDate adapterDate;

    @Override
    public int getLayoutId() {
        return R.layout.activity_gongniu_checkin_new_history;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initView();
        initdata();
    }

    private void initdata() {
        presenter = new GongniuCheckHistoryNewPresenter(this, new GongniuCheckHistoryNewModel());
        ApiGetData(yearSelect,monthSelect);
    }

    public void initView() {
        showDialog = new UtilsShowDialog(this);
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M");
        String dateString = formatter.format(currentTime);
        tvYear.setText(dateString);
        yearSelect = Integer.valueOf(dateString.split("-")[0]);
        monthSelect = Integer.valueOf(dateString.split("-")[1]);
        gvWeek.setAdapter(new AdapterWeek(this));
        textUser.setText(R.string.kaoqinlishi);
    }

    /**
     * 请求考勤数据数据
     */
    private void ApiGetData(int year,int month) {
        Map<String, String> params = new HashMap<>();
        params.put("serviceName", "cquery");
        params.put("objectApiName", "kqxx");
        params.put("binding", RunTimeManager.getInstance().getServerBinding());
        params.put("expressions", "ownerid="+"'"+RunTimeManager.getInstance().getUserId()+"'"
                    +"AND to_char(kqrq,'yyyy-MM-dd') >="+"'"+DateUtil.getFirstDay(year,month)+"'"
                    +"AND to_char(kqrq,'yyyy-MM-dd') <= "+"'"+DateUtil.getLastDay(year,month)+"'");
        Log.e("getFirstDay",DateUtil.getFirstDay(year,month));
        Log.e("getLastDay",DateUtil.getLastDay(year,month));
        presenter.inquireGongniuCheckHistoryList(params);
    }

    @OnClick({R.id.btnImgBack, R.id.tvYear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnImgBack:
                finish();
                break;
            case R.id.tvYear:
                DateTimeUtil dateTimeUtil = new DateTimeUtil();
                dateTimeUtil.initSelectTimePopuwindow_m(mContext,
                        tvYear, false);
                dateTimeUtil.setOnCallBackListener(new DateTimeUtil.onCallBackListener() {
                    @Override
                    public void onTimeSelect(String date) {
                        tvYear.setText(date);
                        yearSelect = Integer.valueOf(date.split("-")[0]);
                        monthSelect = Integer.valueOf(date.split("-")[1]);
                        ApiGetData(yearSelect,monthSelect);
                    }
                });
                break;
        }
    }

    @Override
    public void inquireGongniuCheckHistoryList(BaseListEntity<CheckinHistoryBean> entity) {
        if(checkAdapterkBeans.size()>0){
            checkAdapterkBeans.clear();
            adapterDate.notifyDataSetChanged();
        }

        if(entity.isResult())
        {
            int maxDay = DateUtil.getMaxDay(yearSelect,monthSelect);
            for (int i = 0; i < DateUtil.getFirstDayOfMonth(yearSelect,monthSelect) - 1; i++) {
                //DateUtil.getFirstDayOfMonth()获取当月第一天是星期几，星期日是第一天，依次类推
                CheckAdapterkBean checkAdapterkBean = new CheckAdapterkBean();
                checkAdapterkBean.setDays(0);
                checkAdapterkBeans.add(checkAdapterkBean);
                //0代表需要隐藏的item
//            status.add(false);
                //false代表为签到状态
            }
            for (int i = 0; i < maxDay; i++) {
                CheckAdapterkBean checkAdapterkBean = new CheckAdapterkBean();
                checkAdapterkBean.setDays(i + 1);
                checkAdapterkBeans.add(checkAdapterkBean);
                //初始化日历数据
//            status.add(false);
                //初始化日历签到状态
            }

            //网络数据和日期组合
            for (int i = 0; i < entity.getData().size(); i++) {
                for (int j = 0; j < checkAdapterkBeans.size(); j++) {
                    if (Integer.valueOf(entity.getData().get(i).getKqrq().substring(8,10)) == checkAdapterkBeans.get(j).getDays()) {
                        checkAdapterkBeans.get(j).setCheckinHistoryBean(entity.getData().get(i));
                    }
                }
            }

            adapterDate = new AdapterDate(checkAdapterkBeans,yearSelect,monthSelect, this);
            gvDate.setAdapter(adapterDate);
            adapterDate.setOnSignedSuccess(new OnSignedSuccess() {
                @Override
                public void OnSignedSuccess(int position) {
                    showCheckDetail(false,position);
                }
            });

            showCheckDetail(true,0);
        }
        else {
            ViewInject.toast(entity.getReturnInfo());
        }
    }

    private void showCheckDetail(boolean isFirst,int position)
    {
        if(isFirst) {
            clearData();
            for (int i = 0; i < checkAdapterkBeans.size(); i++) {
                if (checkAdapterkBeans.get(i).getDays() == DateUtil.getCurrentDay(yearSelect,monthSelect)) {
                    if (null != checkAdapterkBeans.get(i).getCheckinHistoryBean()) {
                        if (null != checkAdapterkBeans.get(i).getCheckinHistoryBean().getBgsj()) {
                            amLl.setVisibility(View.VISIBLE);
                            sbworkName.setText("上班");
                            sbtime.setText(checkAdapterkBeans.get(i).getCheckinHistoryBean().getBgsj().
                                    substring(11,checkAdapterkBeans.get(i).getCheckinHistoryBean().getBgsj().length()));
                            sbcontext.setText(checkAdapterkBeans.get(i).getCheckinHistoryBean().getQddz());
                        }
                        if (null != checkAdapterkBeans.get(i).getCheckinHistoryBean().getZwbgsj()) {
                            noonLl.setVisibility(View.VISIBLE);
                            zwworkName.setText("中午");
                            zwtime.setText(checkAdapterkBeans.get(i).getCheckinHistoryBean().getZwbgsj().
                                    substring(11,checkAdapterkBeans.get(i).getCheckinHistoryBean().getZwbgsj().length()));
                            zwcontext.setText(checkAdapterkBeans.get(i).getCheckinHistoryBean().getZwqddz());
                        }
                        if (null != checkAdapterkBeans.get(i).getCheckinHistoryBean().getXbbgsj()) {
                            pmLl.setVisibility(View.VISIBLE);
                            xbworkName.setText("下班");
                            xbtime.setText(checkAdapterkBeans.get(i).getCheckinHistoryBean().getXbbgsj().
                                    substring(11,checkAdapterkBeans.get(i).getCheckinHistoryBean().getXbbgsj().length()));
                            xbcontext.setText(checkAdapterkBeans.get(i).getCheckinHistoryBean().getXbqtdz());
                        }
                    }
                }
            }
        }
        else {
            clearData();
            if(null!= checkAdapterkBeans.get(position).getCheckinHistoryBean()) {
                if (null != checkAdapterkBeans.get(position).getCheckinHistoryBean().getBgsj()) {
                    amLl.setVisibility(View.VISIBLE);
                    sbworkName.setText("上班");
                    sbtime.setText(checkAdapterkBeans.get(position).getCheckinHistoryBean().getBgsj().
                            substring(11, checkAdapterkBeans.get(position).getCheckinHistoryBean().getBgsj().length()));
                    sbcontext.setText(checkAdapterkBeans.get(position).getCheckinHistoryBean().getQddz());
                }
                if (null != checkAdapterkBeans.get(position).getCheckinHistoryBean().getZwbgsj()) {
                    noonLl.setVisibility(View.VISIBLE);
                    zwworkName.setText("中午");
                    zwtime.setText(checkAdapterkBeans.get(position).getCheckinHistoryBean().getZwbgsj().
                            substring(11, checkAdapterkBeans.get(position).getCheckinHistoryBean().getZwbgsj().length()));
                    zwcontext.setText(checkAdapterkBeans.get(position).getCheckinHistoryBean().getZwqddz());
                }
                if (null != checkAdapterkBeans.get(position).getCheckinHistoryBean().getXbbgsj()) {
                    pmLl.setVisibility(View.VISIBLE);
                    xbworkName.setText("下班");
                    xbtime.setText(checkAdapterkBeans.get(position).getCheckinHistoryBean().getXbbgsj().
                            substring(11, checkAdapterkBeans.get(position).getCheckinHistoryBean().getXbbgsj().length()));
                    xbcontext.setText(checkAdapterkBeans.get(position).getCheckinHistoryBean().getXbqtdz());
                }
            }
        }
    }

    private void clearData(){
        amLl.setVisibility(View.GONE);
        noonLl.setVisibility(View.GONE);
        pmLl.setVisibility(View.GONE);
        sbworkName.setText("");
        sbtime.setText("");
        sbcontext.setText("");
        zwworkName.setText("");
        zwtime.setText("");
        zwcontext.setText("");
        xbworkName.setText("");
        xbtime.setText("");
        xbcontext.setText("");
    }

    @Override
    public void showDialog(boolean isShow) {
        if (isShow){
            showDialog.onShowLoadingDialog("正在加载...");
        }
        else {
            showDialog.onDismissLoadingDialog();
        }
    }
}
